﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Web.UI;
using System.Reflection;
using System.Drawing.Design;
using System.Diagnostics;
using System.IO;

namespace Ajaxna.Designers
{
    public class AjaxnaControlActionList : DesignerActionList
    {
        AjaxnaControl _myControl;
        AjaxnaControlDesigner _designer;
        DesignerActionUIService _designerService;
        AddHttpHandlerAction _addWebResourceHandlerAction;
        PropertyDescriptorCollection _properties;


        public AjaxnaControlActionList(IComponent component, AjaxnaControlDesigner designer)
            : base(component)
        {
            _myControl = (AjaxnaControl)component; 
            _designer = designer;
            _designerService = GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
            _properties = TypeDescriptor.GetProperties(component);
        }        


        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();            
            items.Add(new DesignerActionHeaderItem("Configuration"));                     
            //items.Add(new DesignerActionPropertyItem("Scripts", "Scripts", "Configuration"));  
            
            // Action to create WebResource handler
            if (!this.AddWebResourceHandlerAction.HandlerExists)
                items.Add(new DesignerActionMethodItem(this, "AddWebResourceHandler",
                    string.Format("Register {0} as resource handler (required).", _myControl.ResourcesName), "Configuration", true)
                );

            // Add action to view ajax/js.js file for copy/paste.
            items.Add(new DesignerActionMethodItem(this, "ViewReferencesJS", "View Javascript References...", "Configuration", true));

            items.Add(new DesignerActionPropertyItem("ResourcesName", "Resources Name", "Configuration", "The name of the handler in your web.config file used to access Ajax resources"));
            items.Add(new DesignerActionMethodItem(this, "EditScriptsHandler", "Startup Scripts...", true));
            items.Add(new DesignerActionMethodItem(this, "HomePageHandler", "Help and Support...", true));     
            items.Add(new DesignerActionTextItem("Ajaxna 2011", "none"));

            return items;
        }

        private void ViewReferencesJS()
        {            
            // Just use the Text editor directly to display the references JS file content.
            UI.SimpleTextEditorForm form = new UI.SimpleTextEditorForm();
            // Get the text string from this assembly resource
            Assembly assembly = this.GetType().Assembly;
            string resourceName = Resources.TransformPathToResourceName(assembly, "ajax/js/js");            
            string jsString = Resources.ReadResourceString(assembly, resourceName);            
            form.TextValue = jsString;
            form.ShowDialog();
        }

        private void HomePageHandler()
        {            
            Process.Start("http://www.codeplex.com/Ajaxna");
        }

        private void EditScriptsHandler()
        {            
            // We need to invoke the Editor for the Scripts property
            PropertyDescriptor property = TypeDescriptor.GetProperties(this.Component)["Scripts"];
            UITypeEditor editor = TypeDescriptor.GetEditor(property.PropertyType, typeof(UITypeEditor)) as UITypeEditor;
            if (editor != null)
            {                                                               
                TypeDescriptorContext<AjaxnaControlDesigner> context = new TypeDescriptorContext<AjaxnaControlDesigner>(_designer, property);
                editor.EditValue(context, _designer, property.GetValue(this.Component));                
            }
            else
                throw new NullReferenceException("Could not find the editor for type " + property.PropertyType.Name);
                        
        }


        public string ResourcesName
        {
            get { return _properties["ResourcesName"].GetValue(Component) as string;}
            set
            {
                _properties["ResourcesName"].SetValue(Component, value);                
            }
        }        


        protected void AddWebResourceHandler()
        {            
            this.AddWebResourceHandlerAction.AddHttpHandler();                      
        }

        private AddHttpHandlerAction AddWebResourceHandlerAction
        {
            get 
            {
                if (_addWebResourceHandlerAction == null)
                    _addWebResourceHandlerAction = this.CreateAddHttpHandlerAction(_myControl.ResourcesName, typeof(Resources));
                return _addWebResourceHandlerAction;
            }
        }

        private AddHttpHandlerAction CreateAddHttpHandlerAction(string handlerUrl, Type handlerType)
        {
            return new AddHttpHandlerAction(this.Component, handlerUrl, handlerType);
        }
    }
}
